const http = require('../../utils/http.js')
Page({
  data: {
    // 首页传递来的id
    showWineId: 0,
    detail: [],
    //酒品分类与更多商品展示
    classification: [],
    // 图片地址前置
    baseImg: 'https://dianjiu.oss-cn-beijing.aliyuncs.com',
    // 升序or降序
    flagImg: true,
    // 升序图片地址
    shengImg: '../asset/image/other/sheng2.png',
    // 降序图片地址
    jiangImg: '../asset/image/other/jiang2.png',
    // 当前选中栏
    num: '1',
  },
  // 搜索框内容搜索商品
  searchGoods(e) {
    let name = e.detail.value
    let url = ''
    if (name === '') {
      url = `id=` + this.data.showWineId
    } else {
      url = `name=` + name
    }
    http({
      url: '/getWineClassify?' + url,
    })
      .then((res) =>
        this.setData({
          classification: res,
        })
      )
      .catch((res) => console.log(res))
  },
  // 点击综合、销量、新品、价格展示商品
  async showGoods(e) {
    let url = e.currentTarget.dataset.url
    console.log(typeof e.currentTarget.dataset.num)
    this.setData({
      num: e.currentTarget.dataset.num,
    })
    if (url === '/getDescClassify?id=') {
      this.setData({
        flagImg: !this.data.flagImg,
      })
      if (this.data.flagImg) {
        this.setData({
          shengImg: '../asset/image/other/sheng2.png',
          jiangImg: '../asset/image/other/jiang1.png',
        })
      } else {
        this.setData({
          shengImg: '../asset/image/other/sheng1.png',
          jiangImg: '../asset/image/other/jiang2.png',
        })
        url = '/getAscClassify?id='
      }
    }
    let res = await http({
      url: url + this.data.showWineId,
    })
    this.setData({
      classification: res,
    })
  },
  // 跳转到详情
  toDetails(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details',
      events: {
        acceptDataFromOpenedPage: function (data) {},
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { id })
      },
    })
  },
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()
    // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', ({ id }) => {
      this.data.showWineId = id
      http({
        url: '/getWineClassify?id=' + id,
      }).then((res) =>
        this.setData({
          classification: res,
        })
      )
    })
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log(5)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 加载更多，滑动到底部执行此方法
    console.log(1)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
